package com.mew.common.dc.db;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mew.common.config.RedisConstantValue;
import com.mew.common.dc.mapper.BulletinMapper;
import com.mew.common.entity.sys.Bulletin;
import com.mew.common.vo.web.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BulletinService  {

    @Autowired
    private BulletinMapper bulletinMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void lodeBulletin(){
        try {
            List<Object> range = redisTemplate.opsForList().range(RedisConstantValue.ADMIN_BULLETIN, 0, -1);
            for (Object o : range) {
                redisTemplate.opsForList().remove(RedisConstantValue.ADMIN_BULLETIN,0,o);
            }

        } catch (Exception e) {
        }

        List<Bulletin> bulletins = bulletinMapper.selectList(new LambdaQueryWrapper<Bulletin>().eq(Bulletin::getState, 1).orderByAsc(Bulletin::getLayer));
        for (Bulletin bulletin : bulletins) {
            redisTemplate.opsForList().leftPush(RedisConstantValue.ADMIN_BULLETIN,bulletin);
        }
    }

    public List<Bulletin> getRedisBulletins() {
        List<Object> range = redisTemplate.opsForList().range(RedisConstantValue.ADMIN_BULLETIN, 0, -1);
        List<Bulletin> list = range.stream().map(b -> (Bulletin) b).collect(Collectors.toList());
        return list;
    }

}
